package com.base.myproject.client.busi.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.listener.HeardBeatEvent;
import com.base.myproject.client.listener.HeardBeatListener;
import com.base.myproject.client.system.BaseResource;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseHeartBeatX;
import com.extjs.gxt.ui.client.Style.ButtonScale;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

public class Reservation implements HeardBeatListener {
	static Reservation r;
	static ContentPanel l;
	String name;
	 Button hasnew = new Button("新信息", AbstractImagePrototype.create(BaseResource.INSTANCE.newmessage()));  
	static Html html = new Html("");

	private  Reservation(String name) {
		this.name = name;
		//hasnew.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.newmessage()));
		hasnew.setScale(ButtonScale.LARGE);  
		BaseHeartBeatX.getinstance().addHeardBeatListener("Reservation", this);

	}

	static public ContentPanel getInstance(String name) {
		if (r == null) {
			r = new Reservation(name);
			l = new ContentPanel();
			l.add(html);
			ToolBar btb = new ToolBar();
			btb.add(r.hasnew);
			
			r.hasnew.setEnabled(false);
			l.setTopComponent(btb);
			l.add(r.createGrid());
			r.hasnew.addListener(Events.Select, r.getListener());
		}
		return l;
	}

	@Override
	public void HeardBeatEventHandle(HeardBeatEvent evt) {

		html.setHtml(evt.getWhat());
		hasnew.setEnabled(true);
//		if(store!=null)
//			store.reload();
	}

	@Override
	public void onBrowserEvent(Event event) {
		// TODO Auto-generated method stub

	}

	BaseStore<BusiObject> store = null;

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("bcode");
		type.getField("bcode").setType(String.class);
		type.addField("ccode");
		type.getField("ccode").setType(String.class);
		type.addField("Reserdate");
		type.getField("Reserdate").setType(Date.class);
		type.addField("mark");
		type.getField("mark").setType(String.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("Reservation");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select icode,bcode,ccode,Reserdate,mark from Reservation");
		// info.setParent(mainstore);// 关联主表
		// info.setLinkparentcolumns(new String[] { "icode" }); // 关联主表字段
		// info.setLinkparent_chile_columns(new String[] { "upicode" });//
		// 关联主表字段
		store = new BaseStore<BusiObject>(type, info);
		return store;
	}

	BaseGrid grid = null;

	BaseGrid createGrid() {
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

		ColumnConfig C1 = new ColumnConfig("icode", "icode", 80);
		columns.add(C1);
		ColumnConfig C2 = new ColumnConfig("bcode", "bcode", 80);
		columns.add(C2);
		ColumnConfig C3 = new ColumnConfig("ccode", "ccode", 80);
		columns.add(C3);
		ColumnConfig C4 = new ColumnConfig("Reserdate", "Reserdate", 80);
		columns.add(C4);
		ColumnConfig C5 = new ColumnConfig("mark", "mark", 80);
		columns.add(C5);

		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid(getStore(), cm,false,false);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		grid.setSelectionModel(cbs);
		grid.setAutoHeight(true);
		grid.setLoadMask(false);
		return grid;
	}
	int sync = 0;                                                      
	Listener<BaseEvent> defaultlistener;                               
                                                                     
	Listener<BaseEvent> getListener() {                                
		if(defaultlistener!=null)                                        
		{                                                                
			return defaultlistener;                                        
		}                                                                
                                                                     
		defaultlistener = new Listener<BaseEvent>() {                    
			public void handleEvent(BaseEvent be) {                        
				sync++;                                                      
				try {                                                        
					if (sync == 1) {                                           
						if (be.getType() == Events.Select) {                     
							if (be.getSource().equals(hasnew)) {
								if(store!=null)
								store.reload();
								hasnew.setEnabled(false);
							}       
							
						}
						else if (be.getType() == Events.Hide) {    
							
						}
						else if (be.getType() == Events.Change) {              
							if (be.getSource().equals("")) {         
                                                                     
							}                                                      
						} else if (be.getType() == Events.KeyUp) {               
							if (be instanceof ComponentEvent) {                    
								if (((ComponentEvent) be).getKeyCode() == 13) // 回车
								{                                                    
								}                                                    
							}                                                      
						}                                                        
					}                                                          
				} finally {                                                  
					sync--;                                                    
				}                                                            
			}

		                                                       
                                                                     
		};                                                               
		return defaultlistener;                                          
	}   

}
